<?php

namespace App\Domain\HealthCheck\Controllers;

use App\Domain\FamilyMember\RpcService\FamilyMemberInterface;
use App\Domain\HealthCheck\Requests\saveMeasureDataPost;
use Illuminate\Http\Request;
use App\Domain\HealthCheck\Services\HealthCheckService;
use App\Http\Controllers\Controller;
use App\Supports\Constant\PaginationConst;
use App\Supports\Formatter\ResponseFormatter;
use Illuminate\Pagination\LengthAwarePaginator;
use App\Domain\FamilyMember\Dao\FamilyMemberDao;

class HealthCheckController extends Controller
{

//    public function a(FamilyMemberInterface $familyMember){
//        $familyMember->
//            app(FamilyMemberInterface::class)->
//    }
    //保存检测数据


    public function saveMeasureData(saveMeasureDataPost $request, HealthCheckService $healthCheckService)
    {
        $param = $request->validated();
        $data = $healthCheckService->create($param);
        return $this->success($data);
    }

    //测量数据列表
    public function measureDataList(Request $request, HealthCheckService $healthCheckService)
    {
        $params = [
            'uid' => $request['memberUid'],
            'type' => $request['type'],
            'currentPage' => $request->input('currentPage', 1),
            'pageSize' => $request->input('pageSize', PaginationConst::PAGE_SIZE),
        ];
        $healthPaginator = $healthCheckService->list($params);
        //value0Tag,value1Tag,value2Tag字段封装到Paginator
        $items = array_map(function ($val) {
            if ($val['type'] == 0) {
                if ($val['value0'] <= 90) {
                    $val['value0Tag'] = '偏低';
                }
                if ($val['value0'] > 90 && $val['value0'] < 140) {
                    $val['value0Tag'] = '正常';
                }
                if ($val['value0'] >= 140) {
                    $val['value0Tag'] = '偏高';
                }
                if ($val['value1'] <= 60) {
                    $val['value1Tag'] = '偏低';
                }
                if ($val['value1'] > 60 && $val['value1'] < 90) {
                    $val['value1Tag'] = '正常';
                }
                if ($val['value1'] >= 90) {
                    $val['value1Tag'] = '偏高';
                }
                if ($val['value2'] <= 60) {
                    $val['value2Tag'] = '偏低';
                }
                if ($val['value2'] > 60 && $val['value2'] < 100) {
                    $val['value2Tag'] = '正常';
                }
                if ($val['value2'] >= 100) {
                    $val['value2Tag'] = '偏高';
                }
            }
            if ($val['type'] == 1) {
                if ($val['item'] == 0) {
                    if ($val['value0'] <= 3.6) {
                        $val['value0Tag'] = '偏低';
                    }
                    if ($val['value0'] > 3.6 && $val['value0'] < 6.1) {
                        $val['value0Tag'] = '正常';
                    }
                    if ($val['value0'] >= 6.1) {
                        $val['value0Tag'] = '偏高';
                    }
                }
                if ($val['item'] == 1) {
                    if ($val['value0'] <= 7.9) {
                        $val['value0Tag'] = '正常';
                    }
                }
            }
            if ($val['type'] == 2) {
                if ($val['value0'] <= 36) {
                    $val['value0Tag'] = '偏低';
                }
                if ($val['value0'] > 36 && $val['value0'] < 37.3) {
                    $val['value0Tag'] = '正常';
                }
                if ($val['value0'] >= 37 && $val['value0'] <= 38) {
                    $val['value0Tag'] = '发热';
                }
                if ($val['value0'] >= 38.1 && $val['value0'] <= 40) {
                    $val['value0Tag'] = '高烧';
                }
            }
            if ($val['type'] == 3) {
                $height = (new FamilyMemberDao())->findById($val['uid']);
                $heightM = $height['height'] * 0.01;
                $wight = $heightM * $heightM * 22;
                if($val['value0']<$wight){
                    $val['value0Tag'] = '偏低';
                }
                if($val['value0']>$wight){
                    $val['value0Tag'] = '偏高';
                }
            }
            if ($val['type'] == 4) {
                if ($val['value0'] <= 95) {
                    $val['value0Tag'] = '偏低';
                }
                if ($val['value0'] >= 95) {
                    $val['value0Tag'] = '正常';
                }
            }
            return $val;
        }, $healthPaginator->items());
        $healthPaginator = new LengthAwarePaginator($items, $healthPaginator->total(), $healthPaginator->perPage(), $healthPaginator->currentPage());
        //格式化
        $keyMap = [
            'date' => 'create_time',
            'item' => 'item',
            'type' => 'type',
            'value0' => 'value0',
            'value1' => 'value1',
            'value2' => 'value2',
            'value0Tag' => 'value0Tag',
            'value1Tag' => 'value1Tag',
            'value2Tag' => 'value2Tag',
        ];
        return $this->success(ResponseFormatter::formatterPaginator($healthPaginator, $keyMap));
    }

    //图表数据列表
    public function chartDataList(Request $request, HealthCheckService $healthCheckService)
    {
        $params = [
            'uid' => $request['memberUid'],
            'type' => $request['type'],
            'startDate' => $request['startDate'],
            'endDate' => $request['endDate'],
        ];
        $data['list'] = $healthCheckService->chart($params);
        foreach ($data['list'] as $val) {
            if ($val['type'] == 0) {
                if ($val['value0'] <= 90) {
                    $val['value0Tag'] = '偏低';
                }
                if ($val['value0'] > 90 && $val['value0'] < 140) {
                    $val['value0Tag'] = '正常';
                }
                if ($val['value0'] >= 140) {
                    $val['value0Tag'] = '偏高';
                }
                if ($val['value1'] <= 60) {
                    $val['value1Tag'] = '偏低';
                }
                if ($val['value1'] > 60 && $val['value1'] < 90) {
                    $val['value1Tag'] = '正常';
                }
                if ($val['value1'] >= 90) {
                    $val['value1Tag'] = '偏高';
                }
                if ($val['value2'] <= 60) {
                    $val['value2Tag'] = '偏低';
                }
                if ($val['value2'] > 60 && $val['value2'] < 100) {
                    $val['value2Tag'] = '正常';
                }
                if ($val['value2'] >= 100) {
                    $val['value2Tag'] = '偏高';
                }
            }
            if ($val['type'] == 1) {
                if ($val['item'] == 0) {
                    if ($val['value0'] <= 3.6) {
                        $val['value0Tag'] = '偏低';
                    }
                    if ($val['value0'] > 3.6 && $val['value0'] < 6.1) {
                        $val['value0Tag'] = '正常';
                    }
                    if ($val['value0'] >= 6.1) {
                        $val['value0Tag'] = '偏高';
                    }
                }
                if ($val['item'] == 1) {
                    if ($val['value0'] <= 7.9) {
                        $val['value0Tag'] = '正常';
                    }
                }
            }
            if ($val['type'] == 2) {
                if ($val['value0'] <= 36) {
                    $val['value0Tag'] = '偏低';
                }
                if ($val['value0'] > 36 && $val['value0'] < 37.3) {
                    $val['value0Tag'] = '正常';
                }
                if ($val['value0'] >= 37 && $val['value0'] <= 38) {
                    $val['value0Tag'] = '发热';
                }
                if ($val['value0'] >= 38.1 && $val['value0'] <= 40) {
                    $val['value0Tag'] = '高烧';
                }
            }
            if ($val['type'] == 3) {
                $height = (new FamilyMemberDao())->findById($val['uid']);
                $heightM = $height['height'] * 0.01;
                $wight = $heightM * $heightM * 22;
                if($val['value0']<$wight){
                    $val['value0Tag'] = '偏低';
                }
                if($val['value0']>$wight){
                    $val['value0Tag'] = '偏高';
                }
            }
            if ($val['type'] == 4) {
                if ($val['value0'] <= 95) {
                    $val['value0Tag'] = '偏低';
                }
                if ($val['value0'] >= 95) {
                    $val['value0Tag'] = '正常';
                }
            }
            $val['date'] = $val['create_time'];
        }
        return $this->success($data);
    }
}
